package com.example.ywj.myapplication;

import android.os.AsyncTask;

import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
/*
 * 1.传入参数值类型
 * 2.进度值类型
 * 3.返回参数值类型
 * */
public class GetDataTask extends AsyncTask<Void,Void,ArrayList<Apply>> {
    private ApplyAdapter adapter;
    private PullToRefreshListView pullToRefreshListView;

    public GetDataTask(ApplyAdapter adapter,PullToRefreshListView pullToRefreshListView){
        this.pullToRefreshListView = pullToRefreshListView;
    }
    //获取数据
    public ArrayList<Apply> initData(){
        ArrayList<Apply> applys = new ArrayList<>();
        int[] imgs = new int[]{R.drawable.jd,R.drawable.qq,R.drawable.sina,R.drawable.tmall,
                R.drawable.uc,R.drawable.qq_dizhu,R.drawable.weixin};
        String[] names = new String[]{"京东","QQ","新浪","天猫","UC","斗地主","微信"};

        for (int i = 0;i<imgs.length;i++){
            Apply apply = new Apply(imgs[i],names[i]);
            applys.add(apply);
        }
        return applys;
    }
    public void setAdapter(ArrayList<Apply> applys){
        if(adapter == null){
            adapter = new ApplyAdapter(applys);
            pullToRefreshListView.setAdapter(adapter);
        }else{
            // 更新数据
            adapter.notifyDataSetChanged();
        }
    }
    @Override
    protected ArrayList<Apply> doInBackground(Void... voids) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ArrayList<Apply> applys = initData();
        return applys;
    };

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    };

    @Override
    protected void onPostExecute(ArrayList<Apply> applies) {
        setAdapter(applies);
        pullToRefreshListView.onRefreshComplete();
        super.onPostExecute(applies);
    };

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
